package com.lanhe.third.controller;

import cn.hutool.core.util.RandomUtil;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.utils.regex.RegExUtil;
import com.lanhe.common.utils.sms.SmsUtil;
import com.lanhe.verifyRec.domain.LhVerifyRec;
import com.lanhe.verifyRec.service.ILhVerifyRecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 第三方-Oss短信发送
 *
 * @author zxx
 * @date 2023年12月5日
 */
@RestController
@RequestMapping("/api/third/sms")
public class SmsAliController extends BaseController {

	@Autowired
	private  SmsUtil smsUtil;

	@Autowired
	private ILhVerifyRecService verifyRecService;

	/**
	 * @Description 发送登录短信验证码
	 * @MethodName sendLoginCode
	 * @Param [mobile]
	 * @ReturnType com.lanhe.common.core.domain.AjaxResult
	 * @Date 2023/12/5 18:54
	 * @Author zxx
	 * @Since 1.0
	 */
	@GetMapping("/sendLoginCode")
	@ResponseBody
	public AjaxResult sendLoginCode(@RequestParam(value = "mobile", required = true) String mobile,
                                    @RequestParam(value = "type", required = true) Integer type) throws Exception {

		// 1.校验手机号格式
		if (!RegExUtil.matchesMobile(mobile)) {
			return error("手机号格式不正确");
		}
		String code = RandomUtil.randomNumbers(6);
		boolean resMap = smsUtil.sendSMS(mobile, code);
		if (!resMap){
			return error("验证码获取失败，请稍后再试");
		}
		LhVerifyRec verifyRec = new LhVerifyRec();
		verifyRec.setVerifyAccount(mobile);
		verifyRec.setVerifyType(type);
		verifyRec.setVerifyCode(code);
		verifyRec.setVerifyScene(0);
		verifyRecService.addLhVerifyRec(verifyRec);
		return success(resMap);
	}
}
